package com.wolf.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wolf.gulimall.product.dao.BrandDao;
import com.wolf.gulimall.product.dao.CategoryDao;
import com.wolf.gulimall.product.entity.BrandEntity;
import com.wolf.gulimall.product.entity.CategoryEntity;
import com.wolf.gulimall.product.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wolf.common.utils.PageUtils;
import com.wolf.common.utils.Query;

import com.wolf.gulimall.product.dao.CategoryBrandRelationDao;
import com.wolf.gulimall.product.entity.CategoryBrandRelationEntity;
import com.wolf.gulimall.product.service.CategoryBrandRelationService;

@Slf4j
@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询关联信息的详情
     *
     * @param categoryBrandRelation 参数
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);

        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
    }


    /**
     * 根据关联ID和名称修改
     *
     * @param brandId 关联ID
     * @param name    关联名称
     */
    @Override
    public void updateBrand(Long brandId, String name) {
        log.info("进入updateBrand");
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);
        this.update(categoryBrandRelationEntity, new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));


    }

    @Override
    public void updateCascade(Long catId, String name) {
        log.info("进入updateCascade");
        log.info("catId:{},name:{}",catId,name);
        this.baseMapper.updateCatogry(catId, name);
    }

    /**
     * 获取分类关联的品牌
     * @param catId 分类ID
     * @return 品牌信息
     */
    @Override
    public List<BrandEntity> getBrandByCatId(Long catId) {
        List<CategoryBrandRelationEntity> relationEntitles = categoryBrandRelationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        return relationEntitles.stream().map(relationEntitle -> brandService.getById(relationEntitle.getBrandId())).collect(Collectors.toList());
    }

}